package com.bosssoft.goods.controller;

import com.bosssoft.base.entity.Result;
import com.bosssoft.goods.domain.vo.AddGoodVO;
import com.bosssoft.goods.domain.vo.EditGoodInfoVO;
import com.bosssoft.goods.service.GoodsManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 商品管理API接口
 * @author Fang_Sir
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/manage/operate")
@Api(tags = "4-商品管理操作")
public class GoodsManageController {
    @Autowired
    private GoodsManageService goodsManageService;

    @PostMapping("/add/addGoods")
    @ApiOperation(value = "添加新商品")
    public Result<?> addNewGoods(@Valid @RequestBody AddGoodVO info, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        return Result.ok(goodsManageService.addGood(info));
    }

    @PutMapping("/edit/editGoodInfo")
    @ApiOperation(value = "更改商品信息")
    public Result<?> editName(@Valid @RequestBody EditGoodInfoVO info, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        return goodsManageService.editGoodInfo(info) ? Result.ok() : Result.error();
    }

    @DeleteMapping("/edit/delGood/{gid}")
    @ApiOperation(value = "删除商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gid", value = "商品编号", required = true, dataType = "long", paramType = "path")
    })
    public Result<?> deleteGood(@PathVariable("gid") long gid){
        return goodsManageService.deleteGood(gid) ? Result.ok() : Result.error();
    }
}
